/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ContactsGUI.java
 *
 * Created on Jun 13, 2009, 2:20:59 PM
 */
package GUI;

import BUS.ContactBUS;
import BUS.ContactFunction;
import BUS.ContactGroupBUS;
import DTO.ContactDTO;
import DTO.ContactGroupDTO;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.event.MenuDragMouseEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author trquanglong
 */
public class ContactsGUI extends javax.swing.JFrame {

    private DefaultMutableTreeNode _nodeSelected;
    private int _indexNode;
    TreePath[] _treePathSelected;

    /** Creates new form ContactsGUI */
    public ContactsGUI() throws UnsupportedEncodingException {
        initComponents();
        /* Load contact from file data to _lstContact */
        this.setTitle("Contacts");

        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
        lstContact = ContactBUS.getListContact();
        ContactFunction.showTableAllContacts(tableContact, lstContact);
        ContactFunction.showTreeContactPhone(treeContact, ContactBUS.getListContact());
        tableContact.setRowSelectionInterval(0, 0);

        /*Selected node is root*/
        _nodeSelected = (DefaultMutableTreeNode) treeContact.getModel().getRoot();
        _indexNode = 0;
        _treePathSelected = treeContact.getSelectionPaths();


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        bttnNewCard = new javax.swing.JButton();
        bttnNewGroup = new javax.swing.JButton();
        bttnEdit = new javax.swing.JButton();
        bttnDelete = new javax.swing.JButton();
        bttnSearh = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        treeContact = new javax.swing.JTree();
        jTabView = new javax.swing.JTabbedPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableContact = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        panelBusiness = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        panelAddress = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        menuNew = new javax.swing.JMenu();
        menuNewGroup = new javax.swing.JMenuItem();
        menuNewCard = new javax.swing.JMenuItem();
        menuClose = new javax.swing.JMenuItem();
        menuEdits = new javax.swing.JMenu();
        menuSearchCard = new javax.swing.JMenuItem();
        menuEdit = new javax.swing.JMenuItem();
        menuDelete = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        menuBusiness = new javax.swing.JRadioButtonMenuItem();
        menuAddress = new javax.swing.JRadioButtonMenuItem();
        menuPhone = new javax.swing.JRadioButtonMenuItem();
        menuCategory = new javax.swing.JRadioButtonMenuItem();
        menuCompany = new javax.swing.JRadioButtonMenuItem();
        menuLocation = new javax.swing.JRadioButtonMenuItem();
        menuDataFile = new javax.swing.JRadioButtonMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jToolBar1.setBorder(null);
        jToolBar1.setRollover(true);

        bttnNewCard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/addCard.jpg"))); // NOI18N
        bttnNewCard.setMnemonic('1');
        bttnNewCard.setText("Card");
        bttnNewCard.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        bttnNewCard.setFocusable(false);
        bttnNewCard.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bttnNewCard.setMaximumSize(new java.awt.Dimension(80, 80));
        bttnNewCard.setMinimumSize(new java.awt.Dimension(80, 80));
        bttnNewCard.setPreferredSize(new java.awt.Dimension(80, 80));
        bttnNewCard.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        bttnNewCard.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bttnNewCard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttnNewCardActionPerformed(evt);
            }
        });
        jToolBar1.add(bttnNewCard);

        bttnNewGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/address_book_add.gif"))); // NOI18N
        bttnNewGroup.setMnemonic('1');
        bttnNewGroup.setText("Group");
        bttnNewGroup.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        bttnNewGroup.setFocusable(false);
        bttnNewGroup.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bttnNewGroup.setMaximumSize(new java.awt.Dimension(80, 80));
        bttnNewGroup.setMinimumSize(new java.awt.Dimension(80, 80));
        bttnNewGroup.setPreferredSize(new java.awt.Dimension(80, 80));
        bttnNewGroup.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        bttnNewGroup.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bttnNewGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttnNewGroupActionPerformed(evt);
            }
        });
        jToolBar1.add(bttnNewGroup);

        bttnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/editContact.gif"))); // NOI18N
        bttnEdit.setMnemonic('1');
        bttnEdit.setText("Edit");
        bttnEdit.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        bttnEdit.setFocusable(false);
        bttnEdit.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bttnEdit.setMaximumSize(new java.awt.Dimension(80, 80));
        bttnEdit.setMinimumSize(new java.awt.Dimension(80, 80));
        bttnEdit.setPreferredSize(new java.awt.Dimension(80, 80));
        bttnEdit.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        bttnEdit.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bttnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttnEditActionPerformed(evt);
            }
        });
        jToolBar1.add(bttnEdit);

        bttnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/deleteContact.jpg"))); // NOI18N
        bttnDelete.setMnemonic('1');
        bttnDelete.setText("Delete");
        bttnDelete.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        bttnDelete.setFocusable(false);
        bttnDelete.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bttnDelete.setMaximumSize(new java.awt.Dimension(80, 80));
        bttnDelete.setMinimumSize(new java.awt.Dimension(80, 80));
        bttnDelete.setPreferredSize(new java.awt.Dimension(80, 80));
        bttnDelete.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        bttnDelete.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bttnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttnDeleteActionPerformed(evt);
            }
        });
        jToolBar1.add(bttnDelete);

        bttnSearh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/searchContact.gif"))); // NOI18N
        bttnSearh.setMnemonic('1');
        bttnSearh.setText("Search");
        bttnSearh.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        bttnSearh.setFocusable(false);
        bttnSearh.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bttnSearh.setMaximumSize(new java.awt.Dimension(80, 80));
        bttnSearh.setMinimumSize(new java.awt.Dimension(80, 80));
        bttnSearh.setPreferredSize(new java.awt.Dimension(80, 80));
        bttnSearh.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/Save.png"))); // NOI18N
        bttnSearh.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bttnSearh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bttnSearhActionPerformed(evt);
            }
        });
        jToolBar1.add(bttnSearh);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Contacts Book");
        treeContact.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        treeContact.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                treeContactMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(treeContact);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 639, Short.MAX_VALUE)
        );

        jTabView.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabViewStateChanged(evt);
            }
        });

        tableContact.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tableContact.setEnabled(false);
        tableContact.setGridColor(new java.awt.Color(185, 176, 176));
        tableContact.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableContact.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableContactMouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tableContactMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tableContactMouseReleased(evt);
            }
        });
        jScrollPane2.setViewportView(tableContact);

        jTabView.addTab("Normal", jScrollPane2);

        panelBusiness.setEnabled(false);

        javax.swing.GroupLayout panelBusinessLayout = new javax.swing.GroupLayout(panelBusiness);
        panelBusiness.setLayout(panelBusinessLayout);
        panelBusinessLayout.setHorizontalGroup(
            panelBusinessLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1100, Short.MAX_VALUE)
        );
        panelBusinessLayout.setVerticalGroup(
            panelBusinessLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 609, Short.MAX_VALUE)
        );

        jScrollPane3.setViewportView(panelBusiness);

        jTabView.addTab("Business Cards", jScrollPane3);

        panelAddress.setEnabled(false);

        javax.swing.GroupLayout panelAddressLayout = new javax.swing.GroupLayout(panelAddress);
        panelAddress.setLayout(panelAddressLayout);
        panelAddressLayout.setHorizontalGroup(
            panelAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1100, Short.MAX_VALUE)
        );
        panelAddressLayout.setVerticalGroup(
            panelAddressLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 609, Short.MAX_VALUE)
        );

        jScrollPane4.setViewportView(panelAddress);

        jTabView.addTab("Address Cards", jScrollPane4);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabView, javax.swing.GroupLayout.PREFERRED_SIZE, 1107, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTabView, javax.swing.GroupLayout.PREFERRED_SIZE, 639, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jMenu1.setText("File");

        menuNew.setText("New");

        menuNewGroup.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        menuNewGroup.setText("Address Group");
        menuNewGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuNewGroupActionPerformed(evt);
            }
        });
        menuNew.add(menuNewGroup);

        menuNewCard.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        menuNewCard.setText("Address Card");
        menuNewCard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuNewCardActionPerformed(evt);
            }
        });
        menuNew.add(menuNewCard);

        jMenu1.add(menuNew);

        menuClose.setText("Close");
        menuClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCloseActionPerformed(evt);
            }
        });
        jMenu1.add(menuClose);

        jMenuBar1.add(jMenu1);

        menuEdits.setText("Edit");

        menuSearchCard.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        menuSearchCard.setText("Search card");
        menuSearchCard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuSearchCardActionPerformed(evt);
            }
        });
        menuEdits.add(menuSearchCard);

        menuEdit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        menuEdit.setText("Edit card");
        menuEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditActionPerformed(evt);
            }
        });
        menuEdits.add(menuEdit);

        menuDelete.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, 0));
        menuDelete.setText("Delete card (group)");
        menuDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDeleteActionPerformed(evt);
            }
        });
        menuEdits.add(menuDelete);

        jMenuBar1.add(menuEdits);

        jMenu3.setText("View");

        menuBusiness.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_1, java.awt.event.InputEvent.CTRL_MASK));
        menuBusiness.setText("Business Cards");
        menuBusiness.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuBusinessActionPerformed(evt);
            }
        });
        jMenu3.add(menuBusiness);

        menuAddress.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_2, java.awt.event.InputEvent.CTRL_MASK));
        menuAddress.setText("Address Cards");
        menuAddress.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAddressActionPerformed(evt);
            }
        });
        jMenu3.add(menuAddress);

        menuPhone.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_3, java.awt.event.InputEvent.CTRL_MASK));
        menuPhone.setText("Phone Lists");
        menuPhone.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuPhoneActionPerformed(evt);
            }
        });
        jMenu3.add(menuPhone);

        menuCategory.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_4, java.awt.event.InputEvent.CTRL_MASK));
        menuCategory.setText("By Category");
        menuCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCategoryActionPerformed(evt);
            }
        });
        jMenu3.add(menuCategory);

        menuCompany.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_5, java.awt.event.InputEvent.CTRL_MASK));
        menuCompany.setText("By Company");
        menuCompany.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCompanyActionPerformed(evt);
            }
        });
        jMenu3.add(menuCompany);

        menuLocation.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_6, java.awt.event.InputEvent.CTRL_MASK));
        menuLocation.setText("By Location");
        menuLocation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuLocationActionPerformed(evt);
            }
        });
        jMenu3.add(menuLocation);

        menuDataFile.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_7, java.awt.event.InputEvent.CTRL_MASK));
        menuDataFile.setText("Outlook Data Files");
        menuDataFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuDataFileActionPerformed(evt);
            }
        });
        jMenu3.add(menuDataFile);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 719, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCloseActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_menuCloseActionPerformed

    private void menuBusinessActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuBusinessActionPerformed
        // TODO add your handling code here:
        menuBusiness.setSelected(true);
        menuAddress.setSelected(false);
        menuCompany.setSelected(false);
        menuCategory.setSelected(false);
        menuPhone.setSelected(false);
        menuLocation.setSelected(false);
        menuDataFile.setSelected(false);

        /*Show list of contact on jInternal Frames*/
        jTabView.setSelectedIndex(1);

        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
        lstContact = ContactBUS.getListContact();
        ArrayList<ContactGroupDTO> lstGroup = new ArrayList<ContactGroupDTO>();
        lstGroup = ContactGroupBUS.getListContactGroup();

        ContactFunction.showContactBusiness(panelBusiness, lstContact, 0);
        ContactFunction.showTreeContactGroups(treeContact, lstGroup, lstContact);
        tableContact.setRowSelectionInterval(0, 0);
    }//GEN-LAST:event_menuBusinessActionPerformed

    private void menuAddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAddressActionPerformed
        // TODO add your handling code here:
        menuBusiness.setSelected(false);
        menuAddress.setSelected(true);
        menuCompany.setSelected(false);
        menuCategory.setSelected(false);
        menuPhone.setSelected(false);
        menuLocation.setSelected(false);
        menuDataFile.setSelected(false);

        /*Show list of contact on jInternal Frames*/
        jTabView.setSelectedIndex(2);

        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
        lstContact = ContactBUS.getListContact();
        ArrayList<ContactGroupDTO> lstGroup = new ArrayList<ContactGroupDTO>();
        lstGroup = ContactGroupBUS.getListContactGroup();

        ContactFunction.showContactAddress(panelAddress, lstContact, 0);
        ContactFunction.showTreeContactGroups(treeContact, lstGroup, lstContact);
        tableContact.setRowSelectionInterval(0, 0);



    }//GEN-LAST:event_menuAddressActionPerformed

    private void menuPhoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuPhoneActionPerformed
        // TODO add your handling code here
        menuBusiness.setSelected(false);
        menuAddress.setSelected(false);
        menuCompany.setSelected(false);
        menuCategory.setSelected(false);
        menuPhone.setSelected(true);
        menuLocation.setSelected(false);
        menuDataFile.setSelected(false);

        /*Show list of contact on jInternal Frames*/
        jTabView.setSelectedIndex(0);
        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
        lstContact = ContactBUS.getListContact();
        ContactFunction.showTreeContactPhone(treeContact, lstContact);
        ContactFunction.showTableAllContacts(tableContact, lstContact);
        tableContact.setRowSelectionInterval(0, 0);
    }//GEN-LAST:event_menuPhoneActionPerformed

    private void menuCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCategoryActionPerformed
        // TODO add your handling code here:
        /*Show list of contact on jInternal Frames*/
        jTabView.setSelectedIndex(0);


        menuBusiness.setSelected(false);
        menuAddress.setSelected(false);
        menuCompany.setSelected(false);
        menuCategory.setSelected(true);
        menuPhone.setSelected(false);
        menuLocation.setSelected(false);
        menuDataFile.setSelected(false);


        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
        ArrayList<ContactGroupDTO> lstGroup = new ArrayList<ContactGroupDTO>();
        lstGroup = ContactGroupBUS.getListContactGroup();
        lstContact = ContactBUS.getListContact();
        ContactFunction.showTreeContactGroups(treeContact, lstGroup, lstContact);
        ContactFunction.showTableAllContacts(tableContact, lstContact);
        tableContact.setRowSelectionInterval(0, 0);
    }//GEN-LAST:event_menuCategoryActionPerformed

    private void menuCompanyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCompanyActionPerformed
        // TODO add your handling code here
        /*Show list of contact on jInternal Frames*/
        jTabView.setSelectedIndex(0);


        menuBusiness.setSelected(false);
        menuAddress.setSelected(false);
        menuCompany.setSelected(true);
        menuCategory.setSelected(false);
        menuPhone.setSelected(false);
        menuLocation.setSelected(false);
        menuDataFile.setSelected(false);


        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
        ArrayList<String> lstCompany = new ArrayList<String>();
        lstCompany = ContactBUS.getListCompany();
        lstContact = ContactBUS.getListContact();
        ContactFunction.showTreeContactCompany(treeContact, lstCompany, lstContact);
        ContactFunction.showTableAllContacts(tableContact, lstContact);
        tableContact.setRowSelectionInterval(0, 0);
    }//GEN-LAST:event_menuCompanyActionPerformed

    private void menuLocationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuLocationActionPerformed
        // TODO add your handling code here:
        /*Show list of contact on jInternal Frames*/
        jTabView.setSelectedIndex(0);


        menuBusiness.setSelected(false);
        menuAddress.setSelected(false);
        menuCompany.setSelected(false);
        menuCategory.setSelected(false);
        menuPhone.setSelected(false);
        menuLocation.setSelected(true);
        menuDataFile.setSelected(false);
        tableContact.setRowSelectionInterval(0, 0);

        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
        ArrayList<String> lstLocation = new ArrayList<String>();
        lstLocation = ContactBUS.getListLocation();
        lstContact = ContactBUS.getListContact();
        ContactFunction.showTreeContactLocation(treeContact, lstLocation, lstContact);
        ContactFunction.showTableAllContacts(tableContact, lstContact);
        tableContact.setRowSelectionInterval(0, 0);
    }//GEN-LAST:event_menuLocationActionPerformed

    private void menuDataFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuDataFileActionPerformed
        // TODO add your handling code here:
        /*Show list of contact on jInternal Frames*/
        jTabView.setSelectedIndex(0);


        menuBusiness.setSelected(false);
        menuAddress.setSelected(false);
        menuCompany.setSelected(false);
        menuCategory.setSelected(false);
        menuPhone.setSelected(false);
        menuLocation.setSelected(false);
        menuDataFile.setSelected(true);


        ArrayList<String> lstData = new ArrayList<String>();
        lstData = ContactBUS.getListData();
        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
        lstContact = ContactBUS.getListContact();

        ContactFunction.showTreeContactData(treeContact, lstData, lstContact);
        ContactFunction.showTableAllContacts(tableContact, lstContact);
        tableContact.setRowSelectionInterval(0, 0);
    }//GEN-LAST:event_menuDataFileActionPerformed

    /**
     * Add new Card
     * @param evt
     */
    private void bttnNewCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttnNewCardActionPerformed

        // TODO add your handling code here:
        ContactsGUI main = null;
        NewCardGUI dlgContact = null;
        try {
            dlgContact = new NewCardGUI(main, true);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(ContactsGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        dlgContact.setLocationRelativeTo(main);
        dlgContact.show();

        if (dlgContact.isBChange()) {
            if (menuPhone.isSelected()) {
                menuPhoneActionPerformed(evt);
            }

            if (menuCategory.isSelected()) {
                menuCategoryActionPerformed(evt);
            }

            if (menuCompany.isSelected()) {
                menuCompanyActionPerformed(evt);
            }

            if (menuLocation.isSelected()) {
                menuLocationActionPerformed(evt);
            }
            if (menuDataFile.isSelected()) {
                menuDataFileActionPerformed(evt);
            }
        }
    }//GEN-LAST:event_bttnNewCardActionPerformed

    /**
     * Add new Group
     * @param evt
     */
    private void bttnNewGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttnNewGroupActionPerformed
        // TODO add your handling code here:

        // TODO add your handling code here:
        ContactsGUI main = null;
        NewGroupGUI dlgGroup = new NewGroupGUI(main, true);
        dlgGroup.setLocationRelativeTo(main);
        dlgGroup.show();

        String nameGroup = dlgGroup.getNameGroup();
        if (nameGroup.equals("")) {
            return;
        } else {
            try {
                //Add node và cơ sở dữ liệu
                ContactGroupBUS.addContactGroup(nameGroup);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(ContactsGUI.class.getName()).log(Level.SEVERE, null, ex);
            }


            if (menuBusiness.isSelected() || menuAddress.isSelected() || menuCategory.isSelected()) {
                DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeContact.getModel().getRoot();
                DefaultMutableTreeNode nodeGroup;
                nodeGroup = new DefaultMutableTreeNode(nameGroup);
                root.add(nodeGroup);
                ((DefaultTreeModel) treeContact.getModel()).setRoot(root);
            }
        }
        return;
    }//GEN-LAST:event_bttnNewGroupActionPerformed

    /**
     * Click button Edit
     * @param evt
     */
    private void bttnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttnEditActionPerformed
        // TODO add your handling code here:

        ContactDTO selectedContact = new ContactDTO();
        /*Get Name of Selected Contact to Edit*/
        String name = "";
        if (menuCategory.isSelected() || menuCompany.isSelected() || menuDataFile.isSelected() || menuPhone.isSelected() || menuLocation.isSelected()){
            int row = tableContact.getSelectedRow();
            if (row < 0)
                name = "";
            else
                name = (String) tableContact.getValueAt(row, 0);
        }
        if (menuBusiness.isSelected() || menuAddress.isSelected()){
            ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
             if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null) { //Leaf
                    int idGroup = ContactGroupBUS.getIDGroup(_nodeSelected.getParent().toString());
                    lstContact = ContactBUS.getListContactOfGroup(idGroup);
                    int index = _nodeSelected.getParent().getIndex(_nodeSelected);
                    if (lstContact.size() >= 0)
                        name = lstContact.get(index).getName();
                } else {
                    if (_nodeSelected.getParent() != null) { //Group
                        int idGroup = ContactGroupBUS.getIDGroup(_nodeSelected.toString());
                        lstContact = ContactBUS.getListContactOfGroup(idGroup);
                        if (lstContact.size() >= 0)
                            name = lstContact.get(0).getName();
                    } else {
                        if (_nodeSelected.getParent() == null) {//Root
                           lstContact = ContactBUS.getListContact();
                           if (lstContact.size() >= 0)
                                name = lstContact.get(0).getName();
                        }
                    }
                }
        }

        if (name.equals(""))
            JOptionPane.showMessageDialog(menuCompany, "Please choose a contact to edit informations", "Requirement", JOptionPane.INFORMATION_MESSAGE);
        else {
            UpdateContactGUI updateForm = null;
            selectedContact = ContactBUS.getContact(name);
            try {
                updateForm = new UpdateContactGUI(this, rootPaneCheckingEnabled, selectedContact);
                updateForm.show();
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(ContactsGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (updateForm.isIsChange()){
                ContactDTO newContact = updateForm.getContact();
               
                ContactBUS.updateContact( newContact);
                if (menuCategory.isSelected())
                    menuCategoryActionPerformed(evt);
                if (menuPhone.isSelected())
                    menuPhoneActionPerformed(evt);
                if (menuDataFile.isSelected())
                    menuDataFileActionPerformed(evt);
                if (menuAddress.isSelected())
                    menuAddressActionPerformed(evt);
                if (menuBusiness.isSelected())
                    menuBusinessActionPerformed(evt);
                if (menuLocation.isSelected())
                    menuLocationActionPerformed(evt);
                if (menuCompany.isSelected())
                    menuCompanyActionPerformed(evt);
            }


        }
    }//GEN-LAST:event_bttnEditActionPerformed

    /**
     * Click button Delete
     * @param evt
     */
    private void bttnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttnDeleteActionPerformed
        // TODO add your handling code here:
        if (menuPhone.isSelected()){
            if (_nodeSelected.isRoot()){ //Delete All
                int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete all of contacts?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION){
                    ContactBUS.deleteAllContacts();
                    menuPhoneActionPerformed(evt);
                }
            }else{
                if (_nodeSelected.isLeaf()) {//Delete a contacts
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete contact '" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactBUS.deleteContact(_nodeSelected.toString());
                        menuPhoneActionPerformed(evt);
                    }
                }
            }
        }

        if (menuCategory.isSelected() || menuBusiness.isSelected() || menuAddress.isSelected()){
             if (_nodeSelected.isRoot()){ //Delete All
                int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete all of contacts?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION){
                    ContactBUS.deleteAllContacts();
                    if (menuCategory.isSelected())
                        menuCategoryActionPerformed(evt);
                    else
                        if (menuBusiness.isSelected())
                            menuBusinessActionPerformed(evt);
                        else
                            if (menuAddress.isSelected())
                                menuAddressActionPerformed(evt);
                }
            }else{
                 if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null){ //Delete one contact
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete contact '" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactBUS.deleteContact(_nodeSelected.toString());
                         if (menuCategory.isSelected())
                        menuCategoryActionPerformed(evt);
                    else
                        if (menuBusiness.isSelected())
                            menuBusinessActionPerformed(evt);
                        else
                            if (menuAddress.isSelected())
                                menuAddressActionPerformed(evt);
                    }
                 }else{ //Delete Group
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete group '" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactGroupBUS.deleteGroup(_nodeSelected.toString());
                         if (menuCategory.isSelected())
                        menuCategoryActionPerformed(evt);
                    else
                        if (menuBusiness.isSelected())
                            menuBusinessActionPerformed(evt);
                        else
                            if (menuAddress.isSelected())
                                menuAddressActionPerformed(evt);
                    }
                 }

            }
        }


        if (menuLocation.isSelected()){
             if (_nodeSelected.isRoot()){ //Delete All
                int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete all of contacts?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION){
                    ContactBUS.deleteAllContacts();
                    menuLocationActionPerformed(evt);
                }
            }else{
                 if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null){ //Delete one contact
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete contact '" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactBUS.deleteContact(_nodeSelected.toString());
                        menuLocationActionPerformed(evt);
                    }
                 }else{ //Delete Group
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete all contacts of location '" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactBUS.deleteLocation(_nodeSelected.toString());
                        menuLocationActionPerformed(evt);
                    }
                 }

            }
        }

        if (menuCompany.isSelected()){
             if (_nodeSelected.isRoot()){ //Delete All
                int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete all of contacts?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION){
                    ContactBUS.deleteAllContacts();
                    menuCompanyActionPerformed(evt);
                }
            }else{
                 if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null){ //Delete one contact
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete contact '" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactBUS.deleteContact(_nodeSelected.toString());
                        menuCompanyActionPerformed(evt);
                    }
                 }else{ //Delete Group
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete all contacts of company '" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactBUS.deleteCompany(_nodeSelected.toString());
                        menuCompanyActionPerformed(evt);
                    }
                 }
            }
        }

        if (menuDataFile.isSelected()){
            if (_nodeSelected.isRoot()){ //Delete All
                int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete all of contacts?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION){
                    ContactBUS.deleteAllContacts();
                    menuDataFileActionPerformed(evt);
                }
            }else{
                 if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null){ //Delete one contact
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete contact '" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactBUS.deleteContact(_nodeSelected.toString());
                        menuDataFileActionPerformed(evt);
                    }
                 }else{ //Delete Group
                    int option = JOptionPane.showConfirmDialog(menuCompany, "Do you want to delete all contacts of group Data File '<None>" + _nodeSelected.toString() + "'?" , "Confirm Delete", JOptionPane.YES_NO_OPTION);
                    if (option == JOptionPane.YES_OPTION){
                        ContactBUS.deleteDataFile(_nodeSelected.toString());
                        menuDataFileActionPerformed(evt);
                    }
                 }
            }
        }
    }//GEN-LAST:event_bttnDeleteActionPerformed

    /**
     * Search contact on many information
     * @param evt
     */
    private void bttnSearhActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bttnSearhActionPerformed
        // TODO add your handling code here:
        SearchContactGUI formSearch = new SearchContactGUI(this, rootPaneCheckingEnabled);
        formSearch.show();
        
    }//GEN-LAST:event_bttnSearhActionPerformed

    private void tableContactMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableContactMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_tableContactMouseClicked

    private void tableContactMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableContactMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tableContactMousePressed

    private void tableContactMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableContactMouseReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_tableContactMouseReleased

    /**
     * Change selected Tab
     * @param evt
     */
    private void jTabViewStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabViewStateChanged
        // TODO add your handling code here:
        switch (jTabView.getSelectedIndex()) {
            case 0: {
                java.awt.event.ActionEvent tmp = null;
                menuPhoneActionPerformed(tmp);
                break;
            }
            case 1: {
                java.awt.event.ActionEvent tmp = null;
                menuBusinessActionPerformed(tmp);
                break;
            }
            case 2: {
                java.awt.event.ActionEvent tmp = null;
                menuAddressActionPerformed(tmp);
                break;
            }
        }
    }//GEN-LAST:event_jTabViewStateChanged

    /**
     * Click on tree to change node selected
     * @param evt
     */
    private void treeContactMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeContactMouseReleased
        // TODO add your handling code here:
        if (treeContact.getSelectionPath().getLastPathComponent() == _nodeSelected) {
            return;
        } else {
            _nodeSelected = (DefaultMutableTreeNode) treeContact.getSelectionPath().getLastPathComponent();
            java.awt.event.ActionEvent tmpEvent = null;
            if (menuCategory.isSelected()) {

                if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null) {
                    int idGroup = ContactGroupBUS.getIDGroup(_nodeSelected.getParent().toString());
                    ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                    lstContact = ContactBUS.getListContactOfGroup(idGroup);
                    ContactFunction.showTableAllContacts(tableContact, lstContact);

                    int index = _nodeSelected.getParent().getIndex(_nodeSelected);
                    tableContact.setRowSelectionInterval(0, index);
                } else {
                    if (_nodeSelected.getParent() != null) {
                        int idGroup = ContactGroupBUS.getIDGroup(_nodeSelected.toString());
                        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                        lstContact = ContactBUS.getListContactOfGroup(idGroup);
                        ContactFunction.showTableAllContacts(tableContact, lstContact);
                        tableContact.setRowSelectionInterval(0, 0);
                    } else {
                        if (_nodeSelected.getParent() == null) {
                            menuCategoryActionPerformed(tmpEvent);
                            tableContact.setRowSelectionInterval(0, 0);
                        }
                    }
                }

            }


            if (menuLocation.isSelected()) {
                if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null) {
                    ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                    lstContact = ContactBUS.getListContactOfLocation(_nodeSelected.getParent().toString());
                    ContactFunction.showTableAllContacts(tableContact, lstContact);
                    int index = _nodeSelected.getParent().getIndex(_nodeSelected);
                    tableContact.setRowSelectionInterval(0, index);
                } else {
                    if (_nodeSelected.getParent() != null) {
                        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                        lstContact = ContactBUS.getListContactOfLocation(_nodeSelected.toString());
                        ContactFunction.showTableAllContacts(tableContact, lstContact);
                        tableContact.setRowSelectionInterval(0, 0);
                    } else {
                        if (_nodeSelected.getParent() == null) {
                            menuLocationActionPerformed(tmpEvent);
                            tableContact.setRowSelectionInterval(0, 0);
                        }
                    }
                }
            }

            if (menuCompany.isSelected()) {
                if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null) {
                    ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                    lstContact = ContactBUS.getListContactOfCompany(_nodeSelected.getParent().toString());
                    ContactFunction.showTableAllContacts(tableContact, lstContact);
                    int index = _nodeSelected.getParent().getIndex(_nodeSelected);
                    tableContact.setRowSelectionInterval(0, index);

                } else {
                    if (_nodeSelected.getParent() != null) {
                        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                        lstContact = ContactBUS.getListContactOfCompany(_nodeSelected.toString());

                        ContactFunction.showTableAllContacts(tableContact, lstContact);
                        tableContact.setRowSelectionInterval(0, 0);
                    } else {
                        if (_nodeSelected.getParent() == null) {
                            menuCompanyActionPerformed(tmpEvent);
                            tableContact.setRowSelectionInterval(0, 0);
                        }
                    }
                }
            }

            if (menuBusiness.isSelected()) {
                if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null) {
                    //Node leaf
                    int idGroup = ContactGroupBUS.getIDGroup(_nodeSelected.getParent().toString());
                    ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                    lstContact = ContactBUS.getListContactOfGroup(idGroup);
                    int index = _nodeSelected.getParent().getIndex(_nodeSelected);
                    ContactFunction.showContactBusiness(panelBusiness, lstContact, index);

                } else {
                    if (_nodeSelected.getParent() != null) {
                        int idGroup = ContactGroupBUS.getIDGroup(_nodeSelected.toString());
                        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                        lstContact = ContactBUS.getListContactOfGroup(idGroup);
                        ContactFunction.showContactBusiness(panelBusiness, lstContact, 0);
                    } else {
                        if (_nodeSelected.getParent() == null) {
                            menuBusinessActionPerformed(tmpEvent);
                        }
                    }
                }
            }

            if (menuAddress.isSelected()) {
                if (_nodeSelected.getParent() != null && _nodeSelected.getParent().getParent() != null) {
                    //Node leaf
                    int idGroup = ContactGroupBUS.getIDGroup(_nodeSelected.getParent().toString());
                    ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                    lstContact = ContactBUS.getListContactOfGroup(idGroup);
                    int index = _nodeSelected.getParent().getIndex(_nodeSelected);
                    ContactFunction.showContactAddress(panelAddress, lstContact, index);
                } else {
                    if (_nodeSelected.getParent() != null) {
                        int idGroup = ContactGroupBUS.getIDGroup(_nodeSelected.toString());
                        ArrayList<ContactDTO> lstContact = new ArrayList<ContactDTO>();
                        lstContact = ContactBUS.getListContactOfGroup(idGroup);
                        ContactFunction.showContactAddress(panelAddress, lstContact, 0);
                    } else {
                        if (_nodeSelected.getParent() == null) {
                            menuAddressActionPerformed(tmpEvent);
                        }
                    }
                }
            }

            if (menuPhone.isSelected() || menuDataFile.isSelected()) {
                if (_nodeSelected.isLeaf()) {
                    int index = _nodeSelected.getParent().getIndex(_nodeSelected);
                    tableContact.setRowSelectionInterval(0, index);
                } else {
                    tableContact.setRowSelectionInterval(0, 0);
                }
            }
        }
    }//GEN-LAST:event_treeContactMouseReleased

    private void menuSearchCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSearchCardActionPerformed
        // TODO add your handling code here:
        bttnSearhActionPerformed(evt);
    }//GEN-LAST:event_menuSearchCardActionPerformed

    private void menuEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEditActionPerformed
        // TODO add your handling code here:
        bttnEditActionPerformed(evt);
    }//GEN-LAST:event_menuEditActionPerformed

    private void menuDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuDeleteActionPerformed
        // TODO add your handling code here:
        bttnDeleteActionPerformed(evt);
    }//GEN-LAST:event_menuDeleteActionPerformed

    private void menuNewGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuNewGroupActionPerformed
        // TODO add your handling code here:
        bttnNewGroupActionPerformed(evt);
    }//GEN-LAST:event_menuNewGroupActionPerformed

    private void menuNewCardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuNewCardActionPerformed
        // TODO add your handling code here:
        bttnNewCardActionPerformed(evt);
    }//GEN-LAST:event_menuNewCardActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new ContactsGUI().setVisible(true);
                } catch (UnsupportedEncodingException ex) {
                    Logger.getLogger(ContactsGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bttnDelete;
    private javax.swing.JButton bttnEdit;
    private javax.swing.JButton bttnNewCard;
    private javax.swing.JButton bttnNewGroup;
    private javax.swing.JButton bttnSearh;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabView;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JRadioButtonMenuItem menuAddress;
    private javax.swing.JRadioButtonMenuItem menuBusiness;
    private javax.swing.JRadioButtonMenuItem menuCategory;
    private javax.swing.JMenuItem menuClose;
    private javax.swing.JRadioButtonMenuItem menuCompany;
    private javax.swing.JRadioButtonMenuItem menuDataFile;
    private javax.swing.JMenuItem menuDelete;
    private javax.swing.JMenuItem menuEdit;
    private javax.swing.JMenu menuEdits;
    private javax.swing.JRadioButtonMenuItem menuLocation;
    private javax.swing.JMenu menuNew;
    private javax.swing.JMenuItem menuNewCard;
    private javax.swing.JMenuItem menuNewGroup;
    private javax.swing.JRadioButtonMenuItem menuPhone;
    private javax.swing.JMenuItem menuSearchCard;
    private javax.swing.JPanel panelAddress;
    private javax.swing.JPanel panelBusiness;
    private javax.swing.JTable tableContact;
    private javax.swing.JTree treeContact;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the _nodeSelected
     */
    public DefaultMutableTreeNode getNodeSelected() {
        return _nodeSelected;
    }

    /**
     * @param nodeSelected the _nodeSelected to set
     */
    public void setNodeSelected(DefaultMutableTreeNode nodeSelected) {
        this._nodeSelected = nodeSelected;
    }
}
